package alkaid.eis.sys.entity;

import java.util.Comparator;

/**
 * 字典排序
 * 
 * 
 * 
 * @author Lucky Yang
 * @since 0.0.1
 */
public class DictComparator {
    public final static OrderNoAndCodeComparator ORDERNO_AND_CODE = new OrderNoAndCodeComparator();
    public final static DictIdAndOrderNoAndCodeComparator DICTID_AND_ORDERNO_AND_CODE = new DictIdAndOrderNoAndCodeComparator();

    /**
     * 
     * 字典明细排序, 首先使用orderNo排序，然后code排序
     *
     * @author Lucky Yang
     * @since 0.0.1
     */
    public static class OrderNoAndCodeComparator implements Comparator<DictDetail> {
        @Override
        public int compare(DictDetail o1, DictDetail o2) {
            Integer order1 = o1.getOrderNo() == null ? Integer.MAX_VALUE : o1.getOrderNo();
            Integer order2 = o2.getOrderNo() == null ? Integer.MAX_VALUE: o2.getOrderNo();

            // 先排序字段排序
            int rslt = order1.compareTo(order2);
            if (rslt == 0) {
                // 使用代码排序
                return o1.getCode().compareTo(o2.getCode());
            }

            return rslt;
        }
    }

    /**
     * 
     * 字典明细排序, 首先使用dictId排序，然后orderNo排序，然后code排序
     *
     * @author Lucky Yang
     * @since 0.0.1
     */
    public static class DictIdAndOrderNoAndCodeComparator implements Comparator<DictDetail> {
        @Override
        public int compare(DictDetail o1, DictDetail o2) {
            int rslt = o1.getDict().getId().compareTo(o2.getDict().getId());
            if (rslt == 0) {
                return ORDERNO_AND_CODE.compare(o1, o2);
            }

            return rslt;
        }
    }

}
